package com.appbee.core.client.data.test;

import java.util.ArrayList;
import java.util.List;

import com.appbee.core.client.data.IFetcher;
import com.appbee.core.client.data.ISortInfo;
import com.google.gwt.view.client.Range;
import com.google.web.bindery.requestfactory.shared.Receiver;

public class FetcherMock<T> implements IFetcher<T>
{
	private List<Receiver<List<T>>> stack = new ArrayList<Receiver<List<T>>>();
	private List<Range> rangeList = new ArrayList<Range>();
	private List<ISortInfo> sortList = new ArrayList<ISortInfo>();
	private int fetchCount = 0;

	@Override
	public void fetch(Range range, ISortInfo sortInfo,
			Receiver<List<T>> receiver)
	{
		fetchCount++;
		stack.add(receiver);
		rangeList.add(range);
		sortList.add(sortInfo);
	}

	public Range doSuccess(List<T> result)
	{
		if (stack.isEmpty())
			return null;
		Range range = rangeList.remove(0);
		Receiver<List<T>> receiver = stack.remove(0);
		receiver.onSuccess(result);
		return range;
	}

	public int getFetchCount()
	{
		return fetchCount;
	}

	public ISortInfo getLastSortInfo()
	{
		return sortList.get(sortList.size() - 1);
	}
}
